<template>
  <div class="swipe">
    <van-swipe class="swipe-list" :autoplay="3000" indicator-color="white">
      <template v-for="(item, index) in swiperDate">
        <van-swipe-item class="item">
          <img :src="item.url" alt="">
        </van-swipe-item>
      </template>
      <template #indicator="props">
        <div class="indicator">
          <template v-for="(item, key, i) in swipeGroup" :key="key">
            <span class="item" :class="{ active: swiperDate[props.active]?.enumPictureCategory == key }">
              <span class="text">{{ newName(item[0].title) }}</span>
              <span class="count" v-if="swiperDate[props.active]?.enumPictureCategory == key">{{
                getCategoryIndex(swiperDate[props.active]) }}/{{
    item.length }}</span>
            </span>
          </template>
        </div>
      </template>
    </van-swipe>
  </div>
</template>
<script setup>
import { ref } from 'vue';
const swipeGroup = ref({})
const props = defineProps(['swiperDate']);
// 处理脏数据，分成3类
props.swiperDate.forEach(item => {
  swipeGroup.value[item.enumPictureCategory] = []
});
// 根据enumPictureCategory分类数据
Object.keys(swipeGroup.value).forEach(val => {
  props.swiperDate.forEach(item1 => {
    if (val == item1.enumPictureCategory) {
      swipeGroup.value[val].push(item1)
    }
  })
})
// 去【】号
// const reg = /【(.*?)】/i
const newName = (name) => {
  return name.replace("：", "").replace("】", "").replace("【", "")
  // const results = reg.exec(name);
  // console.log(results);
  // return results[1]
}
// 拿到总数据响应的那个对象，去匹配分类里面的对应的那个大类，然后遍历大类去找对应的索引+1
const getCategoryIndex = (item) => {
  const valueArray = swipeGroup.value[item.enumPictureCategory]
  return valueArray.findIndex(data => data === item) + 1
}
// console.log(swipeGroup.value);
// console.log(swipeGroup)
</script>
<style lang="less" scoped>
.swipe {
  font-size: 3.1vw;

  .swipe-list {
    .item {
      img {
        width: 100%;
      }
    }

    .indicator {
      position: absolute;
      right: 5px;
      bottom: 5px;
      display: flex;
      padding: 3px 5px;
      align-items: center;
      font-size: 12px;
      color: #fff;
      background: rgba(0, 0, 0, 0.8);

      .item {
        margin: 0 3px;

        &.active {
          padding: 0px 3px;
          border-radius: 5px;
          background-color: #fff;
          color: #333;
        }
      }
    }
  }
}
</style>
